﻿using Anke.SHManage.BLL.FixedAssetsBLL;
using Anke.SHManage.Model.FixedAssetsModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.FixedAssets.Controllers
{
    public class FixedAssetsScrapController : Controller
    {
        // 固定资产报废
        // GET: /FixedAssets/FixedAssetsScrap/

        /// <summary>
        /// 固定资产报废申请记录列表视图页
        /// </summary>
        /// <returns></returns>
        public ActionResult AssetsScrapList()
        {
            return View("AssetsScrapList");
        }

        /// <summary>
        /// 获取固定资产报废申请单列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetScrapList()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求的页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //获取查询条件
            string assetTypeID = Request.Form["assetTypeID"]; //资产分类编码
            string materialID = Request.Form["materialID"];   //资产编码
            string assetID = Request.Form["assetID"]; //固定资产编号
            string startDate = Request.Form["startDate"];//起始日期
            string endDate = Request.Form["endDate"];//截止日期
            DateTime DateStart = Convert.ToDateTime(startDate);//日期
            DateTime DateEnd = Convert.ToDateTime(endDate).AddDays(1); //日期

            FixedAssetsScrapBLL bll = new FixedAssetsScrapBLL();
            var list = bll.GetScrapList(pageSize, pageIndex, assetTypeID, materialID, assetID, DateStart, DateEnd);
            return Json(list, "appliction/json", JsonRequestBehavior.AllowGet);

        }

        #region 新增/编辑

        /// <summary>
        /// 固定资产报废新增/编辑 视图页
        /// </summary>
        /// <param name="State"></param>
        /// <param name="Code"></param>
        /// <returns></returns>
        public ActionResult ScrapInfoEdit(string State, string Code)
        {
            ViewBag.State = State;
            ViewBag.Code = Code;
            return View("ScrapInfoEdit");
        }

        /// <summary>
        /// 根据报废记录编码获取对应资产报废数据
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public ActionResult GetScrapData(string Code)
        {
            FixedAssetsScrapBLL bll = new FixedAssetsScrapBLL();
            FIXED_ScrapInfo info = bll.GetScrapData(Code);
            return Json(info, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 保存新增/编辑
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public ActionResult SaveEditPageData(FIXED_ScrapInfo info)
        {
            string str = "";
            string state = Request.Form["state"].ToString();
            FixedAssetsScrapBLL bll = new FixedAssetsScrapBLL();
            switch (state)
            {
                case "edit":
                    if (bll.UpdateEditPageData(info))
                    {
                        str = "更新成功";
                    }
                    else
                    {
                        str = "更新失败";
                    }
                    break;
                case "new":
                    //验证该资产编码是否已有报废单
                    if (bll.IsHaveCurrentData(info.ZiChanBianMa))
                    {
                        str = "该资产已有报废单";
                    }
                    else
                    {
                        if (bll.InsertEditPageData(info))
                        {
                            str = "新增成功";
                        }
                        else
                        {
                            str = "新增失败";
                        }
                    }
                    break;
            }
            return Json(str, "text/html", JsonRequestBehavior.AllowGet);
        }


        #endregion

    }
}
